package conchmusic.conchmusicclient.thread;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import org.json.JSONException;

import conchmusic.conchmusicclient.dao.ClientControl;
import conchmusic.conchmusicclient.dao.MyDBHelp;
import conchmusic.conchmusicclient.pojo.UpdatedData;
import conchmusic.conchmusicclient.util.AddSongInListCommand;
import conchmusic.conchmusicclient.util.FindAllSlistCommand;
import conchmusic.conchmusicclient.util.Invoker;
import conchmusic.conchmusicclient.util.JsonParser;

/**
 * Created by Administrator on 2017/4/25.
 */

public class InsertSongToSonglistThread implements Runnable {
    private ClientControl clientControl;
    private MyDBHelp myDBHelp;
    private int uid;
    private FindAllSlistCommand findAllSlistCommand;
    private JsonParser jsonParser;
    private int songid;
    private int songlistid;
    private Context context;
    private AddSongInListCommand addSongInListCommand;
    private Invoker invoker;
    private Handler handler;

    public InsertSongToSonglistThread(int songlistid, int songid, Context context, int uid, Handler handler) {
        this.songlistid = songlistid;
        this.songid = songid;
        this.context = context;
        this.uid=uid;
        invoker=new Invoker();
        jsonParser=new JsonParser();
        this.handler=handler;

    }

    @Override
    public void run() {
        addSongInListCommand=new AddSongInListCommand(context.getResources(),songid,songlistid);
        invoker.setTransferCommand(addSongInListCommand);
        String getResultJson=invoker.action();
        jsonParser=new JsonParser();
        String onlineGetMsg="";
        try {
            String type=jsonParser.typeOf(getResultJson);
            if(type.equals("validInfo")){
                onlineGetMsg=jsonParser.parseValidInfo(getResultJson);
                Log.i("validInfo58",onlineGetMsg);
                updateSongList();
            }else if(type.equals("invalidInfo")){
                onlineGetMsg=jsonParser.parseInvalidInfo(getResultJson);
                handler.sendEmptyMessage(1);
                Log.i("invalidInfo65",onlineGetMsg);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    private void updateSongList() throws JSONException {
        myDBHelp=new MyDBHelp(context,"demon",null,1);
        clientControl=new ClientControl(myDBHelp);
        findAllSlistCommand = new FindAllSlistCommand(context.getResources(), uid);
        invoker.setTransferCommand(findAllSlistCommand);
        String jsonRecive = invoker.action();
        if(jsonParser.typeOf(jsonRecive).equals("invalidInfo")){
            String updateSongInfo=jsonParser.parseInvalidInfo(jsonRecive);
            handler.sendEmptyMessage(2);
            Log.i("updateSongInvalidInfo",jsonParser.parseInvalidInfo(jsonRecive));
        }else {
            UpdatedData ud = jsonParser.parseUpdatedData(jsonRecive);
            clientControl.update(ud);
            handler.sendEmptyMessage(3);

        }
    }
}
